package cn.fl.customer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstCorpShrService;
import cn.fl.customer.transform.CstCorpShrVOTransform;
import cn.fl.customer.vo.CstCorpShrVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.condition.CstCorpShrQueryCondition;
import fl.customer.facade.customer.dto.CstCorpShrDTO;

@Service
public class CstCorpShrServiceImpl implements ICstCorpShrService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CstCorpShrServiceImpl.class);
	
	/**
	 * 客户 API服务
	 */
	@Autowired
	private ICustomerApiService customerApiService;
	
	/**
	 * 本地数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;


	@Override
	public CstCorpShrVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		CstCorpShrDTO dto = this.customerApiService.selectCstCorpShrDTOById(id);
		//设置数据字典项名称
		if(null != dto){
			List<CstCorpShrDTO> dicList = new ArrayList<>();
			dicList.add(dto);
			this.setDicInfo(dicList);
		}
		
		return CstCorpShrVOTransform.toVO(dto);
	}

	@Override
	public List<KeyValueVO> searchShrTypCdList() throws BaseException{
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_SHR_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public Long add(CstCorpShrVO entity) throws BaseException{
		if(null == entity)return null;
		return this.customerApiService.addCstCorpShrDTO(CstCorpShrVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> idList) throws BaseException{
		if(CollectionUtils.isEmpty(idList))return false;
		return this.customerApiService.removeCstCorpShrByIds(idList);
	}


	@Override
	public boolean modify(CstCorpShrVO entity) throws BaseException{
		if(null == entity)return false;
		return this.customerApiService.modifyCstCorpShrDTO(CstCorpShrVOTransform.toDTO(entity));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		Pager pager = this.customerApiService.searchCstCorpShrDTOPage(page);
		List<CstCorpShrDTO> resultList = (List<CstCorpShrDTO>) pager.getResultList();
		this.setDicInfo(resultList);
		pager.setResultList(resultList);
		return pager;
		
	}

	@Override
	public List<KeyValueVO> searchContrWayList() throws BaseException{
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_SHR_CONTR_WAY_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	/**
	 * 设置数据字典项目
	 * @param dto
	 */
	private void setDicInfo(List<CstCorpShrDTO> list) throws BaseException{
		if(CollectionUtils.isEmpty(list))return;
		Map<String, String> shrTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_CST_SHR_TYP_CD);
		Map<String, String> certTypMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		Map<String, String> contrWayMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_CST_SHR_CONTR_WAY_CD);
		for (CstCorpShrDTO cstCorpShrDTO : list) {
			if(shrTypCdMap.containsKey(cstCorpShrDTO.getShrTypCd())){
				cstCorpShrDTO.setShrTypCdNm(shrTypCdMap.get(cstCorpShrDTO.getShrTypCd()));
			}
			
			if(certTypMap.containsKey(cstCorpShrDTO.getCertTypCd())){
				cstCorpShrDTO.setCertTypCdNm(certTypMap.get(cstCorpShrDTO.getCertTypCd()));
			}
			
			if(contrWayMap.containsKey(cstCorpShrDTO.getContrWay())){
				cstCorpShrDTO.setContrWayNm(contrWayMap.get(cstCorpShrDTO.getContrWay()));
			}
		}
	}

	@Override
	public List<CstCorpShrVO> searchList(CstCorpShrQueryCondition qc) throws BaseException{
		if(null == qc ){
			return null;
		}
		return CstCorpShrVOTransform.toVOList(this.customerApiService.searchCstCorpShr(qc));
	}
}
